<?php

namespace App\model;

use Brexis\LaravelWorkflow\Traits\WorkflowTrait;
use Illuminate\Database\Eloquent\Model;
use PhpParser\Builder;
use ZigaStrgar\Orderable\Orderable;
use Intervention\Image\ImageManagerStatic as Image;

/**
 * App\Workshop
 *
 * @property int $id
 * @property string|null $title 厂房标题
 * @property int|null $rent 租金 单位25元/㎡/月
 * @property int|null $workshop_property_type_id 物业类型id
 * @property int|null $area 建筑面积 平方米
 * @property string|null $tag 标签 逗号分隔
 * @property string|null $address 厂房地址
 * @property int|null $built_area 建筑面积 平方米
 * @property float|null $high 首层层高
 * @property float|null $elevator 电梯类型 T
 * @property float|null $distribution 配电类型 kv
 * @property string|null $code 厂房编号
 * @property int|null $workshop_industry_id 厂房行业id
 * @property string|null $desc 厂房描述
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereBuiltArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereDistribution($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereElevator($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereHigh($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereRent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereTag($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereWorkshopIndustryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereWorkshopPropertyTypeId($value)
 * @mixin \Eloquent
 * @property int|null $floor 楼层
 * @property string|null $owndesc 户主描述
 * @property int|null $park_id 园区id
 * @property string|null $ownertel 业主电话
 * @property string|null $owner 业主名称
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereFloor($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereOwndesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereOwner($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereOwnertel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereParkId($value)
 * @property int|null $area_id 面积所属id
 * @property string|null $logo 厂区图片
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereAreaId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereLogo($value)
 * @property int|null $price 价格  5000元/平方米
 * @property int|null $type 类型：0为出租，1为出售
 * @property string|null $slider 轮播图
 * @property string|null $latitude 经度
 * @property string|null $longitude 维度
 * @property-read \App\WorkshopIndustry|null $industry
 * @property-read \App\Park|null $park
 * @property-read \App\WorkshopPropertyType|null $propertyType
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereLatitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereLongitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereSlider($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereType($value)
 * @property string|null $p_province_id
 * @property string|null $p_city_id
 * @property string|null $p_district_id
 * @property string|null $payratio
 * @property string|null $matching_id
 * @property string|null $property
 * @property int|null $house_type
 * @property int|null $is_recommend 是否推荐
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereHouseType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereIsRecommend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereMatchingId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop wherePCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop wherePDistrictId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop wherePProvinceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop wherePayratio($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereProperty($value)
 * @property int|null $click_count 点击量
 * @property int $call_count 电话次数
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereCallCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereClickCount($value)
 * @property string|null $is_out
 * @property string|null $owner_release
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereIsOut($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereOwnerRelease($value)
 * @property string|null $reviewed 审核状态：1待审核，2审核通过
 * @property int|null $is_rent 是否可分租
 * @property float|null $other_floor_high 二层以上层高
 * @property string|null $weight 承重
 * @property int|null $is_park 是否园区
 * @property int|null $certificates 证件
 * @property int|null $status
 * @property string|null $date_of_commission 委托日期
 * @property string|null $commission_end_day 委托时效
 * @property int|null $fuyong 付佣
 * @property \Illuminate\Database\Eloquent\Collection|\App\WorkshopHangye[] $hangye 适用行业
 * @property string|null $extend_tag 自定义标签
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\WorkshopPurpose[] $purpose
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\WorkshopTag[] $tags
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereCertificates($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereCommissionEndDay($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereDateOfCommission($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereExtendTag($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereFuyong($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereHangye($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereIsPark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereIsRent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereOtherFloorHigh($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereReviewed($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Workshop whereWeight($value)
 * @property string|null $lianxi
 * @property-read int|null $hangye_count
 * @property-read int|null $industry_count
 * @property-read int|null $purpose_count
 * @property-read int|null $tags_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereLianxi($value)
 * @property string|null $contact
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereContact($value)
 * @property string|null $owner_company_name 自定义标签
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereOwnerCompanyName($value)
 * @property string|null $geo geohash编码
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereGeo($value)
 * @property-read \App\model\City|null $city
 * @property-read \App\model\City|null $qu
 * @property string|null $hangyes 适用行业
 * @property-read \App\model\City|null $zhen
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereHangyes($value)
 * @property int|null $is_zhiding
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereIsZhiding($value)
 * @property int|null $shenhe
 * @property int|null $user_id 发布者id
 * @property int|null $weight_id 承重id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereShenhe($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereWeightId($value)
 * @property int|null $shenhe_role_id 审核的角色id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereShenheRoleId($value)
 * @property int|null $changfang_type 厂房类型
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Workshop whereChangfangType($value)
 * @property-read \App\model\WorkshopWeight|null $weight_type
 * @property-read \App\User|null $user
 * @property string|null $inside_desc 内部描述
 * @property int|null $is_published 是否发布
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\WorkshopFabu whereInsideDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\WorkshopFabu whereIsPublished($value)
 * @property int|null $department_id 分组
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\WorkshopFabu whereDepartmentId($value)
 */
class WorkshopFabu extends Model
{
    //use WorkflowTrait;
    const RECOMMEND=1;
    const NO_RECOMMEND=0;
    const RENT='元/㎡';
    const SALE='元/㎡';

    //logo大小
    const LOGO='?imageView2/0/w/480/h/360';
    //SLIDER大小
    const SLIDER='?imageView2/0/w/750/h/540';
    public function orderable(){
        return [
            'is_out'=> 'ASC',
            'created_at' => 'DESC',
            'id'
        ];
    }
    public static $rentMap=[
        '分租',
        '整租',
    ];
    public static $parkMap=[
        '不是',
        '是'
    ];
    //审核
    const  SHENHE=1;
    const  SHENHE_PULISHED=2;
    const  SHENHE_UPDATE=3;
    const  SHENHE_REFUSE=4;
    public static $shenheMap=[
        1=>'审核中',
        2=>'审核通过',
        3=>'更新审核',
        4=>'审核未通过',
    ];

    #证件类型
    public static $certificatesMap=[
            '国有土地使用证',
            '房产证',
            '土地证',
            '村租赁合同',
        ];
    #状态
    public static  $statusMap=[
        '有效',
        '无效',
        '已租',
        '我租',
        '已售',
        '我售',
    ];
    #souceMap 来源
    public static $sourceMap=[
        '来电',
        '采访',
        '中介',
        '朋友',
        '广告',
        '网络',
    ];
    public function user(){
       return $this->belongsTo(\App\User::class,'user_id');
    }
    public static $fuyongMap=[
            '给全佣',
            '给半佣',
            '不给佣',
            '商议',
    ];
    //专业厂房
    public static $changfangMap=[
        '轻污染',
        '重污染'
    ];
    public static function boot()
    {
        parent::boot();
//        static::addGlobalScope('shenhe', function ( $builder) {
//            $builder->where('shenhe', '=', self::SHENHE_PULISHED);
//        });
        static::saving(function ($model) {
            if($model->logo==''){
                $arr = json_decode($model->slider,true);
                if ($arr){
                    if(count($arr)>=1 ){
                        $model->logo = $arr[0]['img'];
                    }else{
                        $model->logo = $arr[0]['img'];
                    }
                 }
                }
        });
    }
    //
    protected $table="workshop";
    //用途
    public function purpose()
    {
        return $this->belongsToMany(WorkshopPurpose::class,'workshop_purpose_id','purpose_id','workshop_id');
    }
    //类型
    public function industry(){
        return $this->belongsToMany(WorkshopIndustry::class,'workshop_industry_id','workshop_id','industry_id');
    }
    //标签
    public function tags(){
        return $this->belongsToMany(WorkshopTag::class,'workshop_tag_id','tag_id','workshop_id');
    }
    //行业
    public function hangye(){
        return $this->belongsToMany(WorkshopHangye::class,'workshop_hangye_id','hangye_id','workshop_id');
    }
    //承重
    public function weight_type(){
        return $this->belongsTo(WorkshopWeight::class,'weight_id');
    }
    public function getLogoAttribute($value)
    {
        return $value;
    }
    //市
    public function city(){
      return  $this->belongsTo(City::class,'p_province_id');
    }
    //区
    public function qu(){
        return  $this->belongsTo(City::class,'p_city_id');
    }
    //镇
    public function zhen(){
        return  $this->belongsTo(City::class,'p_district_id');
    }
    public function propertyType(){
        return $this->belongsTo(WorkshopPropertyType::class,'workshop_property_type_id');
    }
    public function park(){

        return $this->belongsTo(Park::class,'park_id');
    }
    public function getContactAttribute($value)
    {
        return '';
    }
    public function setContactAttribute($value)
    {
        $this->attributes['contact'] = '' ;
    }
   public function setLianxiAttribute($value)
    {
        $this->attributes['lianxi'] = implode(',', $value) ;
    }
    public function getLianxiAttribute($value)
    {
        return  array_values(json_decode($value, true) ?: []);
    }


    public function getWorkshopIndustryIdAttribute($value)
    {
        return explode(',', $value);
    }


    public function setWorkshopIndustryIdAttribute($value)
    {
        $this->attributes['workshop_industry_id'] = implode(',', $value);
    }

    public function getMatchingIdAttribute($value)
    {
        return explode(',', $value);
    }

    public function setMatchingIdAttribute($value)
    {
        $this->attributes['matching_id'] = implode(',', $value);
    }
    public static function getSelectOptions()
    {
        $options = \DB::table('workshop')->select('id','title as text')->get();
        $selectOption = [];
        foreach ($options as $option){
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }
}
